package sucre

class AccionMenuController {
    
    def index = { redirect(action:list,params:params) }

    // the delete, save and update actions only accept POST requests
    static allowedMethods = [delete:'POST', save:'POST', update:'POST']

    def list = {
        params.max = Math.min( params.max ? params.max.toInteger() : 10,  100)
        [ accionMenuInstanceList: AccionMenu.list( params ),
            accionMenuInstanceTotal: AccionMenu.count() ]
    }

    def show = {
        def accionMenuInstance = AccionMenu.get( params.id )

        if(!accionMenuInstance) {
            flash.message = "AccionMenu not found with id ${params.id}"
            redirect(action:list)
        }
        else { return [ accionMenuInstance : accionMenuInstance ] }
    }

    def delete = {
        def accionMenuInstance = AccionMenu.get( params.id )
        if(accionMenuInstance) {
            try {
                accionMenuInstance.delete(flush:true)
                flash.message = "AccionMenu ${params.id} deleted"
                redirect(action:list)
            }
            catch(org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "AccionMenu ${params.id} could not be deleted"
                redirect(action:show,id:params.id)
            }
        }
        else {
            flash.message = "AccionMenu not found with id ${params.id}"
            redirect(action:list)
        }
    }

    def edit = {
        def accionMenuInstance = AccionMenu.get( params.id )

        if(!accionMenuInstance) {
            flash.message = "AccionMenu not found with id ${params.id}"
            redirect(action:list)
        }
        else {
            return [ accionMenuInstance : accionMenuInstance ]
        }
    }

    def update = {
        def accionMenuInstance = AccionMenu.get( params.id )
        if(accionMenuInstance) {
            if(params.version) {
                def version = params.version.toLong()
                if(accionMenuInstance.version > version) {
                    
                    accionMenuInstance.errors.rejectValue("version",
                         "accionMenu.optimistic.locking.failure",
                         '''Another user has updated this AccionMenu
                         while you were editing.''')
                    render(view:'edit',model:[accionMenuInstance:
                            accionMenuInstance])
                    return
                }
            }
            accionMenuInstance.properties = params
            if(!accionMenuInstance.hasErrors() && accionMenuInstance.save()) {
                flash.message = "AccionMenu ${params.id} updated"
                redirect(action:show,id:accionMenuInstance.id)
            }
            else {
                render(view:'edit',model:[accionMenuInstance:accionMenuInstance])
            }
        }
        else {
            flash.message = "AccionMenu not found with id ${params.id}"
            redirect(action:list)
        }
    }

    def create = {
        def accionMenuInstance = new AccionMenu()
        accionMenuInstance.properties = params
        return ['accionMenuInstance':accionMenuInstance]
    }

    def save = {
        def accionMenuInstance = new AccionMenu(params)
        if(!accionMenuInstance.hasErrors() && accionMenuInstance.save()) {
            flash.message = "AccionMenu ${accionMenuInstance.id} created"
            redirect(action:show,id:accionMenuInstance.id)
        }
        else {
            render(view:'create',model:[accionMenuInstance:accionMenuInstance])
        }
    }
}
